<?php
/*****************************************************************
 * Fusion.Board
 *
 * @author                Denis Wrobel
 * @license               http://www.gnu.org/licenses/gpl-3.0.html
 * @version               0.1
 *
 *****************************************************************/

if( !defined( 'IN_SCRIPT' ) )
{
    die( 'Go Away!' );
}

$page->title = $language->string( 'log_in' );
$page->addBreadcrumb( 'index.php?action=login', $language->string( 'log_in' ) );

if( $session['session_logged'] )
{
    $page->error( $language->string( 'you_are_already_logged_in' ) );
}

if( $core->_POST( 'submit' ) )
{
    $username = trim( $core->_POST( 'username' ) );
    $password = $core->_POST( 'password' );
    $as_hidden = (int) $core->_POST( 'as_hidden' );
    $remember = ( $core->_POST( 'remember' ) == 'on' ) ? 1 : 0;
    
    if( strlen( $username ) && strlen( $password ) )
    {
        $logon = $user->logIn( $db->escape( $username ), $password, $remember, $as_hidden );
        
        if( $logon == 1 )
        {
            $page->redirect( 'index.php', $language->string( 'you_have_logged_in' ) );
        }
        elseif( !$logon )
        {
            $template->setIf( 'errors' );
            
            $template->assignVars( array( 'l_error_occured' => $language->string( 'error_occured' ),
                                          'error_message'   => $language->string( 'invalid_login_data' ) ) );
        }
        elseif( $logon == 2 )
        {
            $template->setIf( 'errors' );
            
            $template->assignVars( array( 'l_error_occured' => $language->string( 'error_occured' ),
                                          'error_message'   => $language->string( 'account_is_inactive' ) ) );
        }
    }
}


$template->assignVars( array( 'l_log_in'      => $language->string( 'log_in' ),
                              'l_username'    => $language->string( 'username' ),
                              'l_password'    => $language->string( 'password' ),
                              'l_register'    => $language->string( 'register' ),
                              'a_register'    => 'index.php?action=register',
                              'l_as_hidden'   => $language->string( 'login_as_hidden' ),
                              'l_remember_me' => $language->string( 'remember_me' ) ) );

$page->drawHeader();
$template->draw( 'login' );
$page->drawFooter();